home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu108.dms / pu108.adf / WBPat / source / WBPat.c next >
C/C++ Source or Header  |  1993-01-08  |  13KB  |  428 lines

  1.  
  2. /***************************   WBPat.c   ******************************/
  3.  
  4. /////////////////////////// © ASWARE '91 ///////////////////////////////
  5. ///////////////////////// by: Ekke Verheul /////////////////////////////
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <exec/libraries.h>
  10. #include <intuition/intuition.h>
  11. #include <intuition/screens.h>
  12. #include <graphics/gfxbase.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <ctype.h>
  18. #include <dos.h>
  19. #include <libraries/dos.h>
  20. #include <libraries/filehandler.h>
  21. #include <libraries/dosextens.h>
  22. #include <proto/dos.h>
  23. #include <proto/exec.h>
  24. #include <proto/intuition.h>
  25. #include <proto/graphics.h>
  26. #include "win2.data"
  27.  
  28. #define INTUI struct IntuitionBase
  29. #define GFX struct GFXBase
  30. #define LIB struct Library
  31.  
  32. #define MYFLAGS SIMPLE_REFRESH | ACTIVATE | BORDERLESS | WINDOWDRAG | RMBTRAP
  33. #define IDCMP MOUSEBUTTONS | RAWKEY | REFRESHWINDOW | ACTIVEWINDOW | INACTIVEWINDOW
  34. #define WWIDTH  152
  35. #define WHEIGHT 82
  36.  
  37. struct TextAttr Topaz8 = { "topaz.font",8,FS_NORMAL,FPF_ROMFONT };
  38.  
  39. struct NewWindow NewWindow =
  40.     { 0,100,WWIDTH,WHEIGHT,0,1,IDCMP,MYFLAGS,
  41.     NULL,NULL,"WBPat",NULL,NULL,-1,-1,-1,-1,WBENCHSCREEN };
  42.  
  43. UBYTE *testout[] = { "RAM:env/sys/wb.pat","RAM:env/sys/win.pat" };
  44. UBYTE *saveout[] = { "SYS:prefs/Env-Archive/sys/wb.pat","SYS:prefs/Env-Archive/sys/win.pat" };
  45. WORD dest = 0;
  46.  
  47. struct Window *Window = NULL;
  48.  
  49. UBYTE chip buf[84] = {  0x00,0x00,0x00,0x00,
  50.                         0x00,0x00,0x00,0x00,
  51.                         0x00,0x00,0x00,0x00,
  52.                         0x00,0x00,0x00,0x00,
  53.                         0x00,0x40,0x00,0x02, };
  54.  
  55. struct BitMap bm = { 2,16,0,2,0,{ (PLANEPTR)&buf[20],(PLANEPTR)&buf[52], } };
  56.  
  57. UBYTE wbbuf[84],winbuf[84];
  58.  
  59. UBYTE vtag[] = "\0$VER: WBPat 1.0 (10-12-91)";
  60.  
  61.  
  62. /**** proto's
  63. **********************************************************************
  64. ****/
  65.  
  66. void    Interface   (void);
  67. void    NewPat      (ULONG,WORD);
  68. void    FillBuf     (ULONG);
  69. void    PlacePat    (void);
  70. UBYTE   mirror      (UBYTE);
  71. int     GetGadID    (GAD *,WORD,WORD,WORD);
  72. void    SelectGad   (struct RastPort *,GAD *);
  73. void    PaintBits   (struct RastPort *,ULONG );
  74. void    InitBits    (void);
  75.  
  76.  
  77. /**** main
  78. **********************************************************************
  79. ****/
  80. void main(int argc,char **argv)
  81. {
  82. int seed;
  83.  
  84. /* LIBRARIES:  */
  85.     if (IntuitionBase = (INTUI *)OpenLibrary("intuition.library",36))
  86.     {   if (GfxBase = (GFX *)OpenLibrary("graphics.library",36))
  87.         {   NewWindow.LeftEdge = (GfxBase->NormalDisplayColumns - WWIDTH) / 2;
  88.             if (Window = OpenWindow(&NewWindow))
  89.             {   DrawImage(Window->RPort,&image,0,0);
  90.                 SelectGad(Window->RPort,&gads[dest]);
  91.                 time(&seed);
  92.                 srand(seed);
  93.                 Interface();
  94.                 CloseWindow(Window);
  95.             }
  96.             CloseLibrary((LIB *)GfxBase);
  97.         }
  98.         CloseLibrary((LIB *)IntuitionBase);
  99.     }
  100.     XCEXIT(0);
  101. }
  102.  
  103. /**** Interface
  104. **********************************************************************
  105. ****/
  106. void Interface()
  107. {
  108. struct  IntuiMessage *message ;
  109. ULONG   class,t;
  110. WORD code,rs = 32,x,y,qualifier,bit;
  111. WORD id,sel=-1,menu=-1;
  112. BPTR file;
  113.  
  114.     t = rand();
  115.     NewPat(t,rs);
  116.     InitBits();
  117.  
  118.     // KEEP OLD PATTERNS
  119.     if (file = Open(testout[0],1005))
  120.     {   Read(file,wbbuf,84);         // why test? :-)
  121.         Close(file);
  122.     }
  123.     if (file = Open(testout[1],1005))
  124.     {   Read(file,winbuf,84);
  125.         Close(file);
  126.     }
  127.  
  128.     for (;;)
  129.     {   Wait ( 1<<Window->UserPort->mp_SigBit);
  130.         while(message = (struct IntuiMessage *)GetMsg(Window->UserPort))
  131.         {   class     = message->Class  ;
  132.             code      = message->Code;
  133.             x         = message->MouseX;
  134.             y         = message->MouseY;
  135.             qualifier = message->Qualifier;
  136.             ReplyMsg((struct Message *)message);
  137.  
  138.             switch (class)
  139.             {   case INACTIVEWINDOW:
  140.                 case ACTIVEWINDOW:
  141.                 case REFRESHWINDOW:
  142.                 {   DrawImage(Window->RPort,&image,0,0);
  143.                     SelectGad(Window->RPort,&gads[dest]);
  144.                     NewPat(t,rs);
  145.                     break;
  146.                 }
  147.                 case RAWKEY:
  148.                 {   switch (code)
  149.                     {   case 79:   // <-
  150.                         {   rs++;
  151.                             NewPat(t,rs);
  152.                             break;
  153.                         }
  154.                         case 78:   // ->
  155.                         {   rs--;
  156.                             NewPat(t,rs);
  157.                             break;
  158.                         }
  159.                         case 95:   // HELP
  160.                         {   t = rand();
  161.                             rs = 0;
  162.                             NewPat(t,rs);
  163.                             break;
  164.                         }
  165.                         case 16:    // Quit
  166.                         {   goto quit;
  167.                         }
  168.                         case 20:    // Test
  169.                         {   if (file = Open(testout[dest],1006))
  170.                             {   Write(file,buf,84);
  171.                                 Close(file);
  172.                             }
  173.                             break;
  174.                         }
  175.                         case 19:    // Rnd
  176.                         {   t = rand();
  177.                             rs = 0;
  178.                             NewPat(t,rs);
  179.                             break;
  180.                         }
  181.                         case 33:    // Save
  182.                         {   goto save;
  183.                         }
  184.                         case 22:    // Use
  185.                         {   return;
  186.                         }
  187.                     }
  188.                 }
  189.                 case MOUSEBUTTONS:
  190.                 {   id = GetGadID(gads,x,y,GADGETS);
  191.                     if (code == SELECTDOWN && id >= 0)
  192.                     {   SelectGad(Window->RPort,&gads[id]);
  193.                         sel = id;
  194.                         if (id < 2 && id != dest)
  195.                         {   SelectGad(Window->RPort,&gads[id^1]);
  196.                             dest = id;
  197.                             sel = -1;
  198.                         }
  199.                         else if (id == 4)
  200.                         {   rs++;
  201.                             NewPat(t,rs);
  202.                         }
  203.                         else if (id == 5)
  204.                         {   rs--;
  205.                             NewPat(t,rs);
  206.                         }
  207.                     }
  208.                     else if (code == SELECTDOWN && (bit = GetGadID(bits,x,y,32)) >= 0)
  209.                     {   t ^= (0x80000000L >> ((rs+bit) & 31));
  210.                         NewPat(t,rs);
  211.                     }
  212.                     else if (code == SELECTUP && sel >= 0)
  213.                     {   SelectGad(Window->RPort,&gads[sel]);
  214.                         if (id == sel)
  215.                         {   switch(id)
  216.                             {   case 2: // TEST
  217.                                 {   if (file = Open(testout[dest],1006))
  218.                                     {   Write(file,buf,84);
  219.                                         Close(file);
  220.                                     }
  221.                                     break;
  222.                                 }
  223.                                 case 3: // RND
  224.                                 {   t = rand();
  225.                                     rs = 0;
  226.                                     NewPat(t,rs);
  227.                                     break;
  228.                                 }
  229.                                 case 6:  // SAVE
  230.                                 {   save:
  231.                                     if (file = Open(saveout[dest],1006))
  232.                                     {   Write(file,buf,84);
  233.                                         Close(file);
  234.                                     }
  235.                                     return;
  236.                                 }
  237.                                 case 7: // USE
  238.                                 {   return;
  239.                                 }
  240.                                 case 8: // QUIT
  241.                                 {   quit:
  242.                                     if (file = Open(testout[0],1006))
  243.                                     {   Write(file,wbbuf,84);
  244.                                         Close(file);
  245.                                     }
  246.                                     if (file = Open(testout[1],1006))
  247.                                     {   Write(file,winbuf,84);
  248.                                         Close(file);
  249.                                     }
  250.                                     return;
  251.                                 }
  252.                             }
  253.                         }
  254.                         sel = -1;
  255.                     }
  256.                     else if (code == MENUDOWN && id >= 0)
  257.                     {   SelectGad(Window->RPort,&gads[id]);
  258.                         menu = id;
  259.                     }
  260.                     else if (code == MENUUP && menu >= 0)
  261.                     {   SelectGad(Window->RPort,&gads[menu]);
  262.                         menu = -1;
  263.                     }
  264.                     break;
  265.                 }
  266.             }
  267.         }
  268.     }
  269. }
  270.  
  271. /**** NewPat  // create and show new pattern
  272. **********************************************************************
  273. ****/
  274. void NewPat(ULONG rnd,WORD shift)
  275. {
  276. register ULONG t;
  277.  
  278.     shift &= 31;
  279.     t = ( (rnd >> (32-shift)) | (rnd << shift) );
  280.     FillBuf(t);
  281.     PlacePat();
  282.     PaintBits(Window->RPort,t);
  283. }
  284.  
  285. /**** FillBuf // create 64 bytes of bitmat data
  286. **********************************************************************
  287. ****/
  288. void FillBuf(ULONG rnd)
  289. {
  290. register ULONG t;
  291. register WORD i,k;
  292.  
  293.     t = rnd;
  294.     for (i=20,k=0; k<8; k++)
  295.     {   t >>=1;
  296.         buf[102-i] = buf[i++] = (0xFF & t);
  297.         buf[104-i] = buf[i++] = mirror(0xFF & t);
  298.     }
  299.     t = (rnd << 16) | (rnd >> 16);
  300.     for (k=0; k<8;k++)
  301.     {   buf[102-i] = buf[i++] = (0xFF & (t>>8));
  302.         buf[104-i] = buf[i++] = mirror(0xFF & (t>>8));
  303.         t <<= 1;
  304.     }
  305. }
  306.  
  307. /**** mirror // a byte
  308. **********************************************************************
  309. ****/
  310. UBYTE mirror(UBYTE b)
  311. {
  312. register UBYTE r,i;
  313.  
  314.     for (r=0,i=0; i<8; i++)
  315.     {   r <<= 1;
  316.         r |= (b & 0x01);
  317.         b >>= 1;
  318.     }
  319.     return(r);
  320. }
  321.  
  322. /**** PlacePat  //  copy the pattern to our window
  323. **********************************************************************
  324. ****/
  325. void PlacePat()
  326. {
  327. WORD x,y;
  328.  
  329.     WaitTOF();
  330.     for (x = 44; x < 43 + 4 * 16 ; x += 16)
  331.     {   for (y = 17; y < 16 + 3 * 16; y += 16)
  332.         {   BltBitMapRastPort(&bm,0,0,Window->RPort,x,y,16,16,0xC0);
  333.         }
  334.     }
  335. }
  336.  
  337. /**** GetGadID  //  I love those easy gadgets...
  338. **********************************************************************
  339. ****/
  340. int GetGadID(GAD *gad,WORD x,WORD y,WORD gads)
  341. {
  342. register int i;
  343.  
  344.     for (i=0; i<gads; i++)
  345.     {   if ( (gad[i].left < x) &&
  346.                 (gad[i].top < y) &&
  347.                     (gad[i].right > x) &&
  348.                         (gad[i].down > y) )
  349.         {   return(i);
  350.         }
  351.     }
  352.     return(-1);
  353. }
  354.  
  355. /**** SelectGad  //  switch colors of this gadget (black <-> white)
  356. **********************************************************************
  357. ****/
  358. void SelectGad(struct RastPort *rp,GAD *gad)
  359. {
  360. UBYTE lt,rb;
  361.  
  362.     lt = ReadPixel(rp,gad->left,gad->top);
  363.     rb = ReadPixel(rp,gad->right,gad->down);
  364.  
  365.     SetAPen(rp,rb);
  366.     Move(rp,gad->left,gad->down-1);
  367.     Draw(rp,gad->left,gad->top);
  368.     Draw(rp,gad->right-1,gad->top);
  369.  
  370.     SetAPen(rp,lt);
  371.     Move(rp,gad->left+1,gad->down);
  372.     Draw(rp,gad->right,gad->down);
  373.     Draw(rp,gad->right,gad->top+1);
  374. }
  375.  
  376. #define BITSY 71
  377. #define BITSW 2
  378. #define BITSH 3
  379. #define BITSLEFT 12
  380. #define BITSSTEP 4
  381.  
  382. /**** PaintBits  // Hmmm, weird little function (..)
  383. *****            // the painting of 32 white or black blocks.
  384. **********************************************************************
  385. ****/
  386. void PaintBits(struct RastPort *rp,ULONG pat)
  387. {
  388. register ULONG bits = pat,max = 0x80000000L;
  389. register WORD i,x;
  390.  
  391.     for (x = BITSLEFT,i = 0; i<32; i++)
  392.     {   SetAPen(rp,1 + (bits >= max));
  393.         RectFill(rp,x,BITSY,x+BITSW,BITSY+BITSH);
  394.         x += BITSSTEP;
  395.         bits <<= 1;
  396.     }
  397. }
  398.  
  399. /**** InitBits  // calculate the positions of the blocks
  400. **********************************************************************
  401. ****/
  402. void InitBits()
  403. {
  404. register WORD i,x;
  405.  
  406.     for (x = BITSLEFT,i = 0; i<32; i++)
  407.     {   bits[i].left    = x - 1;
  408.         bits[i].top     = BITSY - 2;
  409.         bits[i].right   = x + BITSW + 2;
  410.         bits[i].down    = BITSY + BITSH + 2;
  411.         x += BITSSTEP;
  412.     }
  413. }
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.